<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="BOM物料ID" prop="bomId">
        <el-input
          v-model="queryParams.bomId"
          placeholder="请输入BOM物料ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="父ID" prop="bomParentId">
        <el-input
          v-model="queryParams.bomParentId"
          placeholder="请输入父ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="BOM物料编号" prop="bomCode">
        <el-input
          v-model="queryParams.bomCode"
          placeholder="请输入BOM物料编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="BOM物料名称" prop="bomName">
        <el-input
          v-model="queryParams.bomName"
          placeholder="请输入BOM物料名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="BOM物料版本" prop="bomVersion">
        <el-input
          v-model="queryParams.bomVersion"
          placeholder="请输入BOM物料版本"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="规格型号" prop="bomSpc">
        <el-input
          v-model="queryParams.bomSpc"
          placeholder="请输入规格型号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单位" prop="unitOfMeasure">
        <el-input
          v-model="queryParams.unitOfMeasure"
          placeholder="请输入单位"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物料产品标识" prop="itemOrProduct">
        <el-input
          v-model="queryParams.itemOrProduct"
          placeholder="请输入物料产品标识"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="使用量" prop="used">
        <el-input
          v-model="queryParams.used"
          placeholder="请输入使用量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物料使用比例" prop="proportional">
        <el-input
          v-model="queryParams.proportional"
          placeholder="请输入物料使用比例"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="在库数量" prop="quantityOnhand">
        <el-input
          v-model="queryParams.quantityOnhand"
          placeholder="请输入在库数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单价" prop="price">
        <el-input
          v-model="queryParams.price"
          placeholder="请输入单价"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="生产数量" prop="quantity">
        <el-input
          v-model="queryParams.quantity"
          placeholder="请输入生产数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单据状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择单据状态" clearable>
          <el-option label="请选择字典生成" value=""/>
        </el-select>
      </el-form-item>
      <el-form-item label="" prop="tenantId">
        <el-input
          v-model="queryParams.tenantId"
          placeholder="请输入"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['pro:aomLine:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['pro:aomLine:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['pro:aomLine:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['pro:aomLine:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border v-loading="loading" :data="aomLineList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="行ID" align="center" prop="id"/>
      <el-table-column label="父ID" align="center" prop="parentId"/>
      <el-table-column label="物料分析编码" align="center" prop="aomId"/>
      <el-table-column label="物料分析编码" align="center" prop="aomCode"/>
      <el-table-column label="物料分析名称" align="center" prop="aomName"/>
      <el-table-column label="生产计划ID" align="center" prop="planId"/>
      <el-table-column label="计划编码" align="center" prop="planCode"/>
      <el-table-column label="计划名称" align="center" prop="planName"/>
      <el-table-column label="产品ID" align="center" prop="productId"/>
      <el-table-column label="产品编号" align="center" prop="productCode"/>
      <el-table-column label="产品名称" align="center" prop="productName"/>
      <el-table-column label="BOM物料ID" align="center" prop="bomId"/>
      <el-table-column label="父ID" align="center" prop="bomParentId"/>
      <el-table-column label="BOM物料编号" align="center" prop="bomCode"/>
      <el-table-column label="BOM物料名称" align="center" prop="bomName"/>
      <el-table-column label="BOM物料版本" align="center" prop="bomVersion"/>
      <el-table-column label="规格型号" align="center" prop="bomSpc"/>
      <el-table-column label="单位" align="center" prop="unitOfMeasure"/>
      <el-table-column label="物料产品标识" align="center" prop="itemOrProduct"/>
      <el-table-column label="使用量" align="center" prop="used"/>
      <el-table-column label="物料使用比例" align="center" prop="proportional"/>
      <el-table-column label="在库数量" align="center" prop="quantityOnhand"/>
      <el-table-column label="单价" align="center" prop="price"/>
      <el-table-column label="生产数量" align="center" prop="quantity"/>
      <el-table-column label="单据状态" align="center" prop="status"/>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="" align="center" prop="tenantId"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['pro:aomLine:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['pro:aomLine:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改生产计划物料分析对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="父ID" prop="parentId">
          <el-input v-model="form.parentId" placeholder="请输入父ID"/>
        </el-form-item>
        <el-form-item label="物料分析编码" prop="aomId">
          <el-input v-model="form.aomId" placeholder="请输入物料分析编码"/>
        </el-form-item>
        <el-form-item label="物料分析编码" prop="aomCode">
          <el-input v-model="form.aomCode" placeholder="请输入物料分析编码"/>
        </el-form-item>
        <el-form-item label="物料分析名称" prop="aomName">
          <el-input v-model="form.aomName" placeholder="请输入物料分析名称"/>
        </el-form-item>
        <el-form-item label="生产计划ID" prop="planId">
          <el-input v-model="form.planId" placeholder="请输入生产计划ID"/>
        </el-form-item>
        <el-form-item label="计划编码" prop="planCode">
          <el-input v-model="form.planCode" placeholder="请输入计划编码"/>
        </el-form-item>
        <el-form-item label="计划名称" prop="planName">
          <el-input v-model="form.planName" placeholder="请输入计划名称"/>
        </el-form-item>
        <el-form-item label="产品ID" prop="productId">
          <el-input v-model="form.productId" placeholder="请输入产品ID"/>
        </el-form-item>
        <el-form-item label="产品编号" prop="productCode">
          <el-input v-model="form.productCode" placeholder="请输入产品编号"/>
        </el-form-item>
        <el-form-item label="产品名称" prop="productName">
          <el-input v-model="form.productName" placeholder="请输入产品名称"/>
        </el-form-item>
        <el-form-item label="BOM物料ID" prop="bomId">
          <el-input v-model="form.bomId" placeholder="请输入BOM物料ID"/>
        </el-form-item>
        <el-form-item label="父ID" prop="bomParentId">
          <el-input v-model="form.bomParentId" placeholder="请输入父ID"/>
        </el-form-item>
        <el-form-item label="BOM物料编号" prop="bomCode">
          <el-input v-model="form.bomCode" placeholder="请输入BOM物料编号"/>
        </el-form-item>
        <el-form-item label="BOM物料名称" prop="bomName">
          <el-input v-model="form.bomName" placeholder="请输入BOM物料名称"/>
        </el-form-item>
        <el-form-item label="BOM物料版本" prop="bomVersion">
          <el-input v-model="form.bomVersion" placeholder="请输入BOM物料版本"/>
        </el-form-item>
        <el-form-item label="规格型号" prop="bomSpc">
          <el-input v-model="form.bomSpc" placeholder="请输入规格型号"/>
        </el-form-item>
        <el-form-item label="单位" prop="unitOfMeasure">
          <el-input v-model="form.unitOfMeasure" placeholder="请输入单位"/>
        </el-form-item>
        <el-form-item label="物料产品标识" prop="itemOrProduct">
          <el-input v-model="form.itemOrProduct" placeholder="请输入物料产品标识"/>
        </el-form-item>
        <el-form-item label="使用量" prop="used">
          <el-input v-model="form.used" placeholder="请输入使用量"/>
        </el-form-item>
        <el-form-item label="物料使用比例" prop="proportional">
          <el-input v-model="form.proportional" placeholder="请输入物料使用比例"/>
        </el-form-item>
        <el-form-item label="在库数量" prop="quantityOnhand">
          <el-input v-model="form.quantityOnhand" placeholder="请输入在库数量"/>
        </el-form-item>
        <el-form-item label="单价" prop="price">
          <el-input v-model="form.price" placeholder="请输入单价"/>
        </el-form-item>
        <el-form-item label="生产数量" prop="quantity">
          <el-input v-model="form.quantity" placeholder="请输入生产数量"/>
        </el-form-item>
        <el-form-item label="单据状态">
          <el-radio-group v-model="form.status">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="" prop="tenantId">
          <el-input v-model="form.tenantId" placeholder="请输入"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addAomLine, delAomLine, getAomLine, listAomLine, updateAomLine } from "@/api/mes/pro/aomLine";

export default {
  name: "AomLine",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 生产计划物料分析表格数据
      aomLineList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        bomId: null,
        bomParentId: null,
        bomCode: null,
        bomName: null,
        bomVersion: null,
        bomSpc: null,
        unitOfMeasure: null,
        itemOrProduct: null,
        used: null,
        proportional: null,
        quantityOnhand: null,
        price: null,
        quantity: null,
        status: null,
        tenantId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        aomId: [
          {required: true, message: "物料分析编码不能为空", trigger: "blur"}
        ], aomCode: [
          {required: true, message: "物料分析编码不能为空", trigger: "blur"}
        ], planId: [
          {required: true, message: "生产计划ID不能为空", trigger: "blur"}
        ], planCode: [
          {required: true, message: "计划编码不能为空", trigger: "blur"}
        ], productId: [
          {required: true, message: "产品ID不能为空", trigger: "blur"}
        ], productCode: [
          {required: true, message: "产品编号不能为空", trigger: "blur"}
        ], productName: [
          {required: true, message: "产品名称不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询生产计划物料分析列表 */
    getList() {
      this.loading = true;
      listAomLine(this.queryParams).then(response => {
        this.aomLineList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        parentId: null,
        aomId: null,
        aomCode: null,
        aomName: null,
        planId: null,
        planCode: null,
        planName: null,
        productId: null,
        productCode: null,
        productName: null,
        bomId: null,
        bomParentId: null,
        bomCode: null,
        bomName: null,
        bomVersion: null,
        bomSpc: null,
        unitOfMeasure: null,
        itemOrProduct: null,
        used: null,
        proportional: null,
        quantityOnhand: null,
        price: null,
        quantity: null,
        status: "0",
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        tenantId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加生产计划物料分析";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getAomLine(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改生产计划物料分析";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs[ "form" ].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAomLine(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAomLine(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除生产计划物料分析编号为"' + ids + '"的数据项？').then(function () {
        return delAomLine(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('pro/aomLine/export', {
        ...this.queryParams
      }, `aomLine_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
